package com.yyds.blackmagic.service.impl;

import com.yyds.blackmagic.entity.AdminMenu;
import com.yyds.blackmagic.entity.AdminPermission;
import com.yyds.blackmagic.entity.AdminRole;
import com.yyds.blackmagic.mapper.AdminMenuMapper;
import com.yyds.blackmagic.mapper.AdminPermissionMapper;
import com.yyds.blackmagic.mapper.AdminRoleMapper;
import com.yyds.blackmagic.service.AdminMenuService;
import com.yyds.blackmagic.service.AdminPermissionService;
import com.yyds.blackmagic.service.AdminRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author ccc
 * @Date 2021/12/17
 * @Descripton
 */
@Service
@Slf4j
public class AdminRoleServiceImpl implements AdminRoleService {
    @Resource
    AdminRoleMapper adminRoleMapper;
    @Autowired
    AdminPermissionService adminPermissionService;
    @Autowired
    AdminMenuService adminMenuService;

    @Override
    public List<AdminRole> findAll () {
        List<AdminRole> roles = adminRoleMapper.findAll ();
        List<AdminPermission> perms;
        List<AdminMenu> menus;
        for (AdminRole role : roles) {
            perms = adminPermissionService.findPermsByRoleId (role.getId ());
            menus = adminMenuService.getMenusByRoleId (role.getId ());
            role.setPerms (perms);
            role.setMenus (menus);
        }
        return roles;
    }

    @Override
    public Integer updateRoleStatus (AdminRole requestRole) {
        log.info ("role:" + requestRole.toString ());
        int res = adminRoleMapper.updateStatus (requestRole);
        return res;
    }

    @Override
    public AdminRole add (AdminRole requestRole) {
        int res = adminRoleMapper.add (requestRole);
        return requestRole;
    }

    @Override
    public int update (AdminRole requestRole) {
        int res =  adminRoleMapper.update(requestRole);
        return res;
    }

    @Override
    public int updateRoleMenu (int rid, Map<String, List<Integer>> menusIds) {
        adminRoleMapper.deleteAllMenuByRid(rid);
//        List<AdminRoleMenu> rms = new ArrayList<> ();
        int res = 0;
        for (Integer mid : menusIds.get("menusIds")) {
            res+= adminMenuService.add(rid,mid);
        }

        return res;
    }
}
